package com.snake.mylibrary.db_provider;

import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;

import com.snake.mylibrary.utils.LogUtil;


public class DBProvider extends ContentProvider {

    private static final String SCHEMA = "content://";
    private static final String AUTHORITIES = "com.snake.mylibrary.db_provider.DBProvider";
    private static final int TABLE_TYPE1 = 1;
    private static final int TABLE_TYPE2 = 2;
    private static final int TABLE_TYPE3 = 3;
    private static UriMatcher sUriMatcher;

    private SQLiteDatabase mDb;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(AUTHORITIES, DBConfig.NAME1,TABLE_TYPE1);
        sUriMatcher.addURI(AUTHORITIES,DBConfig.NAME2,TABLE_TYPE2);
        sUriMatcher.addURI(AUTHORITIES,DBConfig.NAME3,TABLE_TYPE3);
    }


    //被访问的时候调用
    @Override
    public boolean onCreate() {
        DBOpenHelper dbHelper = new DBOpenHelper(getContext(), DBConfig.DB_NAME, null, DBConfig.DB_VERSION);
        mDb = dbHelper.getWritableDatabase();
        LogUtil.e("TestProvider创建了");
        return false;
    }

    @Nullable
    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        LogUtil.e("query："+uri);
        Cursor cursor  = null;
        switch (sUriMatcher.match(uri)){
            case TABLE_TYPE1:
                cursor = mDb.query(DBConfig.NAME1,projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case TABLE_TYPE2:
                cursor = mDb.query(DBConfig.NAME2,projection,selection,selectionArgs,null,null,sortOrder);
                break;
            case TABLE_TYPE3:
                cursor = mDb.query(DBConfig.NAME3,projection,selection,selectionArgs,null,null,sortOrder);
                break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(Uri uri) {
        String type = null;
        switch (sUriMatcher.match(uri)){
            case TABLE_TYPE1:
                type = SCHEMA+AUTHORITIES+"/"+DBConfig.NAME1;
                break;
            case TABLE_TYPE2:
                type = SCHEMA+AUTHORITIES+"/"+DBConfig.NAME2;
                break;
            case TABLE_TYPE3:
                type = SCHEMA+AUTHORITIES+"/"+DBConfig.NAME3;
                break;
        }
        return type;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri, ContentValues values) {
        LogUtil.e("insert："+uri);
        switch (sUriMatcher.match(uri)){
            case TABLE_TYPE1:
                mDb.insert(DBConfig.NAME1,null,values);
                break;
            case TABLE_TYPE2:
                mDb.insert(DBConfig.NAME2,null,values);
                break;
            case TABLE_TYPE3:
                mDb.insert(DBConfig.NAME3,null,values);
                break;
        }
        notifyChange(uri);
        return null;
    }

    private void notifyChange(Uri uri) {
        ContentResolver resolver = getContext().getContentResolver();
        if(resolver!=null){
            resolver.notifyChange(uri,null);
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        LogUtil.e("delete："+uri);
        switch (sUriMatcher.match(uri)){
            case TABLE_TYPE1:
                mDb.delete(DBConfig.NAME1,selection,selectionArgs);
                break;
            case TABLE_TYPE2:
                mDb.delete(DBConfig.NAME2,selection,selectionArgs);
                break;
            case TABLE_TYPE3:
                mDb.delete(DBConfig.NAME3,selection,selectionArgs);
                break;
        }
        notifyChange(uri);
        return 0;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        LogUtil.e("update："+uri);
        switch (sUriMatcher.match(uri)){
            case TABLE_TYPE1:
                mDb.update(DBConfig.NAME1,values,selection,selectionArgs);
                break;
            case TABLE_TYPE2:
                mDb.update(DBConfig.NAME2,values,selection,selectionArgs);
                break;
            case TABLE_TYPE3:
                mDb.update(DBConfig.NAME3,values,selection,selectionArgs);
                break;
        }
        notifyChange(uri);
        return 0;
    }
}
